library("foreign");library("randtests");library("spgs")

setwd("~/Dropbox/Florence_Paper/replication/data") 



data<-read.dta("avg_test_currency2.dta")


data<-data[data$year > 1393,]




#data<-data[!is.na(data$gondf),]





export<-ifelse(data$gondf==22|data$gondf==23,1,0)
export[data$year > 1393]



lb_frame<-data.frame( matrix(NA, 4 ,10))


for(i in 1:10){

a<-lb.test(as.factor(data$gondf),i)

lb_frame[1,i]<-round(a$statistic,3)
lb_frame[2,i]<-paste("(", round(a$p.value,3), ")",sep="")





b<-lb.test(data$export,i)

lb_frame[3,i]<-round(b$statistic,3)
lb_frame[4,i]<-paste("(", round(b$p.value,3), ")",sep="")




}



lb_frame2<-data.frame(matrix("&",4,20))

for(i in 1:10){
  
  
lb_frame2[, i*2-1 ] <- lb_frame[,i]  
  

names(lb_frame2)[i*2-1]<-paste("lag:",i,sep="")


names(lb_frame2)[i*2]<- ""




}


lb_frame2[,20]<-"\\"


#### Table 2 ######

print(lb_frame2)



#### Figure A2 #####


pdf("~/Dropbox/Florence_Paper/replication/figures/runs_test.pdf",12,12)


par(las=1,mar=c(3,6.5,4,1))
plot(data$time-19,data$export,axes=F,pch=16,col=  ifelse(data$export==1,"red","blue")   ,xlab="",ylab="")
grid()



for(i in 2:nrow(data)){
  if(  diff(data$export)[i] !=0 |  is.na(diff(data$export)[i]) ) {
    
    lines(c(data$time[i-1]-19, data$time[i-1]-19  ), c(0,1),lty=2)
    
    
  }  

  
  
  
  
}


axis(1,seq(0,250,by=25));axis(2,at=c(0,1),labels=c("Non-Exporter","Exporter"))
mtext("Wald-Wolfowitz Runs Test",3,cex=1.5,line=1.5)
mtext("-0.325; p-value: (0.745)")
runs.test(export, threshold = .5)

dev.off()




kruskal.test(data$export ~ time, data = data) 




#######################


## Chi square test on page 17 footnote 34

chisq.test(prop.table(table(data[data$year > 1393, ]$scrutiny,data[data$year > 1393, ]$gondf),1),correct=T)





